package com.example.stupidweather.utils;

import androidx.annotation.Nullable;

import com.example.stupidweather.gson.Location;
import com.example.stupidweather.gson.Weather;
import com.google.gson.Gson;

import org.json.JSONObject;

public class Utility {
    /**
     * 传入json数据,返回实例化后的Weather对象
     *
     * @param response 传入的json数据
     * @return 实例化后的Weather对象
     */
    @Nullable
    public static Weather handleWeatherResponse(String response) {
        try {

            // 将整个json实例化保存在jsonObject中
            JSONObject jsonObject = new JSONObject(response);

            // 返回通过Gson解析后的Weather对象
            Gson g=new Gson();
            Weather weather=  g.fromJson(jsonObject.toString(),Weather.class);

            return weather;

        } catch (Exception e) {
            e.printStackTrace();


        }
        return null;

    }

    /**
     * 传入json数据,返回实例化后的Location对象
     *
     * @param response 传入的json数据
     * @return 实例化后的Location对象
     */
    @Nullable
    public static Location handleLocationResponse(String response) {
        try {

            // 将整个json实例化保存在jsonObject中
            JSONObject jsonObject = new JSONObject(response);

            // 返回通过Gson解析后的Weather对象
            Gson g=new Gson();
            Location location = g.fromJson(jsonObject.toString(), Location.class);

            return location;

        } catch (Exception e) {
            e.printStackTrace();


        }
        return null;

    }

}
